package com.ruoyi.restaurant.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.restaurant.domain.ResShop;
import com.ruoyi.restaurant.service.IResShopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.restaurant.domain.ResCategory;
import com.ruoyi.restaurant.service.IResCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜品分类Controller
 * 
 * @author adnaan
 * @date 2023-12-08
 */
@RestController
@RequestMapping("/restaurant/category")
public class ResCategoryController extends BaseController
{
    @Autowired
    private IResCategoryService resCategoryService;

    @Autowired
    private IResShopService resShopService;

    /**
     * 查询菜品分类列表
     */
    @PreAuthorize("@ss.hasPermi('restaurant:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResCategory resCategory)
    {
        Long userId = SecurityUtils.getUserId();
        if(!SecurityUtils.isAdmin(userId)){
            ResShop resShop = new ResShop();
            resShop.setOwnerId(userId);
            List<ResShop> list = resShopService.selectResShopList(resShop);
            if(list.isEmpty()){
                System.out.println("该用户没有店铺");
            }else{
                ResShop shop = list.get(0);
                resCategory.setShopId(shop.getId());
            }
        }
        startPage();
        List<ResCategory> list = resCategoryService.selectResCategoryList(resCategory);
        return getDataTable(list);
    }

    /**
     * 导出菜品分类列表
     */
    @PreAuthorize("@ss.hasPermi('restaurant:category:export')")
    @Log(title = "菜品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResCategory resCategory)
    {
        List<ResCategory> list = resCategoryService.selectResCategoryList(resCategory);
        ExcelUtil<ResCategory> util = new ExcelUtil<ResCategory>(ResCategory.class);
        util.exportExcel(response, list, "菜品分类数据");
    }

    /**
     * 获取菜品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('restaurant:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(resCategoryService.selectResCategoryById(id));
    }

    /**
     * 新增菜品分类
     */
    @PreAuthorize("@ss.hasPermi('restaurant:category:add')")
    @Log(title = "菜品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResCategory resCategory)
    {
        Long userId = SecurityUtils.getUserId();
        if(!SecurityUtils.isAdmin(userId)){
            ResShop resShop = new ResShop();
            resShop.setOwnerId(userId);
            List<ResShop> list = resShopService.selectResShopList(resShop);
            if(list.isEmpty()){
                System.out.println("该用户没有店铺");
            }else{
                ResShop shop = list.get(0);
                resCategory.setShopId(shop.getId());
            }
            return toAjax(resCategoryService.insertResCategory(resCategory));
        }else {
            return error("超级管理员不能添加菜品分类。");
        }
    }

    /**
     * 修改菜品分类
     */
    @PreAuthorize("@ss.hasPermi('restaurant:category:edit')")
    @Log(title = "菜品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResCategory resCategory)
    {
        return toAjax(resCategoryService.updateResCategory(resCategory));
    }

    /**
     * 删除菜品分类
     */
    @PreAuthorize("@ss.hasPermi('restaurant:category:remove')")
    @Log(title = "菜品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(resCategoryService.deleteResCategoryByIds(ids));
    }
}
